package util

import (
	"encoding/base64"
	"fmt"
	"strings"
)

func DecodeBasicAuth(authHeader string) (string, string, error) {
	// 去除 "Basic " 前缀
	authHeader = strings.TrimPrefix(authHeader, "Basic ")

	// 解码 base64 字符串
	decoded, err := base64.StdEncoding.DecodeString(authHeader)
	if err != nil {
		return "", "", err
	}

	// 分割用户名和密码
	parts := strings.Split(string(decoded), ":")
	if len(parts) != 2 {
		return "", "", fmt.Errorf("invalid basic auth header")
	}

	return parts[0], parts[1], nil
}
